package com.fdsc.erp.weixin.WeixinOauth2;

import com.fdsc.erp.weixin.Utils.CommonUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Description:网页授权工具类
 * @Author:lishanhui
 * @Date:Create in 10:59  2018-06-07
 **/
public class WeixinOauth2TokenUtil {
    //网页授权链接
    public static String OAUTH2URL = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
            "appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect";

    /**
     * @Date:Create in 11:11 2018-06-07
     * @Description:获取网页授权凭证
     * @params: * @param
     * @return:
     **/
    public static WeixinOauth2Token getOauthToken(String appId, String appSecret, String code) {
        WeixinOauth2Token wat = null;
        //拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        requestUrl = requestUrl.replace("APPID", appId);
        requestUrl = requestUrl.replace("SECRET", appSecret);
        requestUrl = requestUrl.replace("CODE", code);
        //获取网页授权凭证
        try {
            String resp = CommonUtil.doGet(requestUrl);
            JSONObject object = JSONObject.fromObject(resp);
            wat = new WeixinOauth2Token();
            wat.setAccessToken(object.getString("access_token"));
            wat.setExpiresIn(object.getInt("expires_in"));
            wat.setOpenId(object.getString("openid"));
            wat.setRefreshToken(object.getString("refresh_token"));
            wat.setScope(object.getString("scope"));
        } catch (Exception e) {
            e.printStackTrace();
            wat = null;
        }
        return wat;
    }

    /**
     * @Date:Create in 11:11 2018-06-07
     * @Description:刷新网页授权凭证
     * @params: * @param
     * @return:
     **/
    public static WeixinOauth2Token refreshOauthToken(String appId, String refreshToken) {
        WeixinOauth2Token wat = null;
        //拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";
        requestUrl = requestUrl.replace("APPID", appId);
        requestUrl = requestUrl.replace("REFRESH_TOKEN", refreshToken);
        //刷新网页授权凭证
        try {
            String resp = CommonUtil.doGet(requestUrl);
            JSONObject object = JSONObject.fromObject(resp);
            wat = new WeixinOauth2Token();
            wat.setAccessToken(object.getString("access_token"));
            wat.setExpiresIn(object.getInt("expires_in"));
            wat.setOpenId(object.getString("openid"));
            wat.setRefreshToken(object.getString("refresh_token"));
            wat.setScope(object.getString("scope"));
        } catch (Exception e) {
            e.printStackTrace();
            wat = null;
        }
        return wat;
    }

    /**
     * @Date:Create in 11:11 2018-06-07
     * @Description:通过网页授权获取 用户信息
     * @params: * @param
     * @return:
     **/
    @SuppressWarnings({"deprecation", "unchecked"})
    public static SNSUserInfo getSNSUserInfo(String accessToken, String openId) {
        SNSUserInfo snsUserInfo = null;
        //拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);
        requestUrl = requestUrl.replace("OPENID", openId);
        //通过网页授权获取 用户信息
        try {
            String resp = CommonUtil.doGet(requestUrl);
            JSONObject object = JSONObject.fromObject(resp);
            snsUserInfo = new SNSUserInfo();
            snsUserInfo.setOpenId(object.getString("openid"));
            snsUserInfo.setCity(object.getString("city"));
            snsUserInfo.setCountry(object.getString("country"));
            snsUserInfo.setHeadImgUrl(object.getString("headimgurl"));
            snsUserInfo.setNickName(object.getString("nickname"));
            snsUserInfo.setPrivilegeList(JSONArray.toList(object.getJSONArray("privilege"), List.class));
            snsUserInfo.setProvince(object.getString("province"));
            snsUserInfo.setSex(object.getString("sex"));
        } catch (Exception e) {
            e.printStackTrace();
            snsUserInfo = null;
        }
        return snsUserInfo;
    }

    /**
     * @Date:Create in 13:33 2018-06-07
     * @Description:URL编码 （utf-8）
     * @params: * @param source
     * @return:
     **/
    public static String urlEncdeUTF8(String source) {
        String result = source;
        try {
            result = java.net.URLEncoder.encode(source,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
}
